var gLoginUsername = "${loginUsername}"; // 获得当前登录人员的userName
var gDeptUid = '${request.getParameter("deptUid")}';
var calenarUid;

// @Bind #deptTree.onDataRowClick
!function(self, arg, dsCalendar) {
	var dropDown = dorado.widget.DropDown.findDropDown(self);
	var node = self.get("currentNode");
	var calendar = dsCalendar.getData("#");

	calendar.set("deptUid", node.get("data").get("uuid"));
	calendar.set("dept.deptName", node.get("data").get("deptName"));

	dropDown.close(node.get("data"));
}

// @Bind #dpCalendar.onValueSelect
!function(self, arg, dsBaseCalendar, dsCalendar) {
	var baseCalendar = dsBaseCalendar.getData("#");
	var calendar = dsCalendar.getData("#");

	calendar.set("baseCalendarUid", baseCalendar.get("calendarUid"));
	calendar.set("baseCalendar.calendarName", baseCalendar.get("calendarName"));
}

// @Bind #dsDept.onReady
!function(self, arg) {
	var dsDept = view.id("dsDept");
	var user = view.id("dsUser").getData("#");
	
	dsDept.set("parameter", user.get("deptUid"));
}

// @Bind #dsBaseCalendar.onReady
!function(self, arg) {
	var dsBaseCalendar = view.id("dsBaseCalendar");
	var user = view.id("dsUser").getData("#");
	
	dsBaseCalendar.set("parameter", {
		//deptUid : user.get("deptUid")
		deptUid : "001306"
	});

}

// @Bind @dtCalendarData.onInsert
!function(self, arg, dsCalendar) {
	var calendar = dsCalendar.getData("#");
	var calendarData = arg.entity;
	
	if (calenarUid){
		calendarData.set("calendarUid", calenarUid);
	}else {
		calendarData.set("calendarUid", calendar.get("calendarUid"));
	}

}

//@Bind @dtCalendar.beforeInsert
!function(self, arg, dsCalendar) {
	var calendar = arg.entity;
	calenarUid = uuid();
	calendar.set("calendarUid", calenarUid);

}

//生成uuid
function uuid() {
    var s = [];
    var hexDigits = "0123456789abcdef";
    for (var i = 0; i < 36; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
    }
    s[14] = "4";  // bits 12-15 of the time_hi_and_version field to 0010
    s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);  // bits 6-7 of the clock_seq_hi_and_reserved to 01
    s[8] = s[13] = s[18] = s[23] = "-";
 
    var uuid = s.join("");
    return uuid;
}
